package org.bspv.pharma.web.util.tags;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.bspv.pharma.web.bean.node.Node;
import org.bspv.pharma.web.bean.node.visitor.VisitorManager;

/**
 * This is a simple tag example to show how content is added to the output
 * stream when a tag is encountered in a JSP page.
 */
public class TreeTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -272183059455828650L;
	private String name = null;
	private Node node = null;
	private String edit = "false";
	private String select = "false";
	private String standardEquipment = "false";

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the node
	 */
	public Node getNode() {
		return node;
	}

	/**
	 * @param node
	 *            the node to set
	 */
	public void setNode(Node node) {
		this.node = node;
	}

	/**
	 * @return the edit
	 */
	public String getEdit() {
		return edit;
	}

	/**
	 * @param edit the edit to set
	 */
	public void setEdit(String edit) {
		this.edit = edit;
	}

	/**
	 * @return the select
	 */
	public String getSelect() {
		return select;
	}

	/**
	 * @param select the select to set
	 */
	public void setSelect(String select) {
		this.select = select;
	}
	
	/**
	 * @return the standardEquipment
	 */
	public String getStandardEquipment() {
		return standardEquipment;
	}

	/**
	 * @param standardEquipment the standardEquipment to set
	 */
	public void setStandardEquipment(String standardEquipment) {
		this.standardEquipment = standardEquipment;
	}

	/**
	 * doStartTag is called by the JSP container when the tag is encountered
	 */
	public int doStartTag() {
		try {
			JspWriter out = pageContext.getOut();
			out.println("<ul id=\"placeNode\" name=\"\" class=\"treeview-gray\">");
			// affichage du noeud courant et de ses sous noeuds
			for (Node child: node.getChildren()) {
				out.println(evaluateNode(child));
			}
		} catch (Exception ex) {
			throw new Error("Oups.");
		}
		// Must return SKIP_BODY because we are not supporting a body for this
		// tag.
		return SKIP_BODY;
	}

	/**
	 * doEndTag is called by the JSP container when the tag is closed
	 */
	public int doEndTag() {
		try {
			JspWriter out = pageContext.getOut();
			out.println("</ul>");
		} catch (Exception ex) {
			throw new Error("Oups.");
		}
		return (EVAL_PAGE);
	}

	/**
	 * Renvoie la chaine correspondant � l'arbre
	 * 
	 * @param node
	 * @return
	 */
	private String evaluateNode(Node node) {
		//construction de la tables des parametres
		String path = ((HttpServletRequest)pageContext.getRequest()).getContextPath();
		Map<String, String> params = new HashMap<String, String>();
		params.put("path", path);
		params.put("edit", edit);
		params.put("select", select);
		params.put("standardEquipment", standardEquipment);
		//appel du visiteur
		return VisitorManager.getVisitor(node.getClass()).visit(node, params);
	}

}